package com.linghui.videoplus.ipai.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linghui.videoplus.ipai.R;
import com.linghui.videoplus.ipai.VideoPlusApplication;
import com.linghui.videoplus.ipai.api.Preferences;
import com.linghui.videoplus.ipai.utils.Constants;

public class SettingActivity extends BaseActivity implements OnTouchListener {

	private LinearLayout loginOrOutLL;
	private LinearLayout myHomeLL;
	private LinearLayout sendMailLL;
	private LinearLayout attentionLL;
	
	private TextView loginOrOutTV;
	private ImageView loginOrOutIV;
	private ImageView backIV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setting);

		loginOrOutLL = (LinearLayout) findViewById(R.id.loginOrOutLL);
		loginOrOutLL.setOnTouchListener(this);
		
		myHomeLL=(LinearLayout)findViewById(R.id.myHomeLL);
		myHomeLL.setOnTouchListener(this);
		
		sendMailLL=(LinearLayout)findViewById(R.id.sendMailLL);
		sendMailLL.setOnTouchListener(this);
		
		attentionLL =(LinearLayout)findViewById(R.id.attentionLL);
		attentionLL.setOnTouchListener(this);
		
		loginOrOutTV= (TextView)findViewById(R.id.loginOrOutTV);
		loginOrOutIV= (ImageView)findViewById(R.id.loginOrOutIV);
		
		backIV =(ImageView) findViewById(R.id.backIV);
		backIV.setOnTouchListener(this);
		if (VideoPlusApplication.isLogin(this)) {
			loginOrOutTV.setText(R.string.logout);
			loginOrOutIV.setBackgroundResource(R.drawable.setting_logout);
		} else {
			loginOrOutTV.setText(R.string.login_two);
			loginOrOutIV.setBackgroundResource(R.drawable.setting_login);
		}

	}
	
	@Override
	protected void onStart() {
		if (VideoPlusApplication.isLogin(this)) {
			loginOrOutTV.setText(R.string.logout);
			loginOrOutIV.setBackgroundResource(R.drawable.setting_logout);
			
		} else {
			loginOrOutTV.setText(R.string.login_two);
			loginOrOutIV.setBackgroundResource(R.drawable.setting_login);
		}
		super.onStart();
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		if(v==loginOrOutLL){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				loginOrOutLL.setBackgroundResource(R.drawable.bg_blank_down);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				loginOrOutLL.setBackgroundResource(R.drawable.bg_blank_up);
				if (VideoPlusApplication.isLogin(this)) {
					doLogout();
				}else{
					AccountActivity.launch(this, REQUEST_CODE_SETTING,REQUEST_CODE_ACCOUNT,null);
				}
			}
			return true;
		}else if(v==myHomeLL){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				myHomeLL.setBackgroundResource(R.drawable.bg_blank_down);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				myHomeLL.setBackgroundResource(R.drawable.bg_blank_up);
				if(VideoPlusApplication.isLogin(this)){
					Intent intent = new Intent();
					intent.setClass(this, CommWebViewActivity.class);
					intent.putExtra("url", VideoPlusApplication.getUrlList(this));
					intent.putExtra(FROM_CODE_KEY, REQUEST_CODE_SETTING);
					intent.putExtra(TO_CODE_KEY, REQUEST_CODE_DEFAULT);
					this.startActivity(intent);
				}else{
					super.showToast(getResources().getString(R.string.hint_no_login));
				}
			}
			return true;
		}
		else if(v==sendMailLL){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				sendMailLL.setBackgroundResource(R.drawable.bg_blank_down);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				sendMailLL.setBackgroundResource(R.drawable.bg_blank_up);
				Intent intent=new Intent(Intent.ACTION_SEND);  
	            intent.setType("message/rfc822");   
	            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"linkwise@linghui.com"});  
	            intent.putExtra(Intent.EXTRA_SUBJECT,"看看爱拍用户反馈");  
	            intent.putExtra(Intent.EXTRA_TEXT, "反馈将会通过email发送给我们,您可以自由编辑这封email,然后点击发送！\t\n您的反馈意见:\t\n");  
	            startActivity(Intent.createChooser(intent,"反馈"));
			}
			return true;
		}else if(v==attentionLL){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				attentionLL.setBackgroundResource(R.drawable.bg_blank_down);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				attentionLL.setBackgroundResource(R.drawable.bg_blank_up);
				Intent intent = new Intent();
				intent.setClass(this, SinaWebViewActivity.class);
				intent.putExtra("url", Constants.KANKAN_SINA_WEIBO);
				startActivityForResult(intent, RESULT_OK);
			}	
			return true;
			
		}else if(v==backIV){
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				backIV.setBackgroundResource(R.drawable.btn_back_dwon);
			}else if (event.getAction() == MotionEvent.ACTION_UP) {
				backIV.setBackgroundResource(R.drawable.btn_back_up);
				VideoCascadeActivity.launch(this,REQUEST_CODE_SETTING, REQUEST_CODE_VIDEO_CASCADE);
			}	
			return true;
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
//			SettingActivity.this.setResult(RESULT_OK, getIntent());
//			SettingActivity.this.finish();
			VideoCascadeActivity.launch(this,REQUEST_CODE_SETTING, REQUEST_CODE_VIDEO_CASCADE);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 退出操作
	 */
	private void doLogout(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("您确定退出账户?");
		builder.setCancelable(false);
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Preferences.cleanAccount(SettingActivity.this);
				loginOrOutTV.setText(R.string.login_two);
				loginOrOutIV.setBackgroundResource(R.drawable.setting_login);
				SettingActivity.this.showToast("已成功退出!");
			}
		});
		builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	
	public static void launch(Activity mActivity,int fromCode,int toCode) {
		Intent intent = new Intent(mActivity, SettingActivity.class);
		intent.putExtra(FROM_CODE_KEY, fromCode);
		intent.putExtra(TO_CODE_KEY, toCode);
		mActivity.startActivity(intent);
		mActivity.finish();
	}
	
}
